﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using INTEX.DataBase.Tools;
using System.Data.SQLite;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class XSGB02 : PDCBase
    {
        BaseFunction m_Data;
        public string fromdate = "", enddate = "";        
        #region=====  vwGLLG=======
        CollectionViewSource vwGLLG
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGLLG");
            }
        }
        DataRow dwGLLG
        {
            get
            {
                if (vwGLLG.View == null)
                    return null;
                if ((DataRowView)vwGLLG.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGLLG.View.CurrentItem).Row;
            }
        }
        #endregion

        public XSGB02()
        {
            InitializeComponent();
            m_Data = new BaseFunction();
           
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (vm == null)
                {
                    vm = this.DataContext as MainViewModel;
                }
                string _date = "", _time = "", _stime = "",startdat = "",enddat = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                //startdat = System.DateTime.Today.AddDays(-1).Year.ToString()
                //        + System.DateTime.Today.AddDays(-1).Month.ToString().PadLeft(2, '0')
                //        + System.DateTime.Today.AddDays(-1).Day.ToString().PadLeft(2, '0');
                //enddat = System.DateTime.Today.AddDays(-1).Year.ToString()
                //        + System.DateTime.Today.AddDays(-1).Month.ToString().PadLeft(2, '0')
                //        + System.DateTime.Today.AddDays(-1).Day.ToString().PadLeft(2, '0');
                FROM_DATE.Text = System.DateTime.Today.AddDays(0).Year.ToString()
                            + "-" + System.DateTime.Today.AddDays(0).Month.ToString()
                            + "-" + System.DateTime.Today.AddDays(0).Day.ToString();
                END_DATE.Text = System.DateTime.Today.AddDays(0).Year.ToString()
                            + "-" + System.DateTime.Today.AddDays(0).Month.ToString()
                            + "-" + System.DateTime.Today.AddDays(0).Day.ToString();
                fromdate =  System.DateTime.Today.AddDays(0).Year.ToString()
                            + System.DateTime.Today.AddDays(0).Month.ToString().PadLeft(2, '0')
                            + System.DateTime.Today.AddDays(0).Day.ToString().PadLeft(2, '0');
                enddate = System.DateTime.Today.AddDays(0).Year.ToString()
                          + System.DateTime.Today.AddDays(0).Month.ToString().PadLeft(2, '0')
                          + System.DateTime.Today.AddDays(0).Day.ToString().PadLeft(2, '0');
                vm.GLLO = "01";
                vm.GLLB = "XS";
                vm.GLLP = "F       ";
                vm.SPINLOT = "H";//140161016";
                /*if (vm.app.User.Trim() == "ITXV")
                {
                    vm.SPINLOT = "H";//140161016";
                    vm.GLLO = "01";
                    vm.GLLB = "CP";
                    vm.GLLP = "A       ";
                    vm.DATES = "20140101";
                  
                }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            
            DataRow dwData = SetEnv.ICC_GB1109.NewRow();
            dwData["GLLO"] = vm.GLLO;
            dwData["GLLB"] = vm.GLLB;
            dwData["GLLP"] =Helper.Left( vm.GLLP.PadRight(8),8);
            dwData["GPSPPART"] = vm.SPINLOT;
            dwData["NEWDATE"] = fromdate;
            dwData["CHGDATE"] = enddate;
            vm.LoadGLLG(dwData);
        }

        private void btnToConvert_Click(object sender, RoutedEventArgs e)
        {
            if (dwGLLG == null)
                return;
            string GPSPPART = dwGLLG["GPSPPART"].ToString();

            string filter = "GPSPPART='" + GPSPPART + "' AND GLLBEHI <> '9999999999' ";
            DataRow[] rows=SetEnv.Tables["GLLG"].Select(filter);
            vwGLLG.View.MoveCurrentTo(-1);
            DataTable tb=rows.CopyToDataTable();
            tb.TableName = "GLLG";
            SetData.Merge(tb);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            SetEnv.AcceptChanges();
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            SetProd.Clear();
            SetEnv.Merge(SetData);
            SetData.Clear();
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
